package com.example.demo.service;

import com.example.demo.dao.TBizArtUserDAO;
import com.example.demo.po.PageRequest;
import com.example.demo.po.PageResult;
import com.example.demo.po.TBizArtUser;
import com.example.demo.po.UserManage;
import com.example.demo.util.DateUtil;
import com.example.demo.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class TBizArtUserService {
    @Autowired
    TBizArtUserDAO tbaUserDao;

    /**
     * 分页查询接口
     * 这里统一封装了分页请求和结果，避免直接引入具体框架的分页对象, 如MyBatis或JPA的分页对象
     * 从而避免因为替换ORM框架而导致服务层、控制层的分页接口也需要变动的情况，替换ORM框架也不会
     * 影响服务层以上的分页接口，起到了解耦的作用
     * @param pageRequest 自定义，统一分页查询请求
     * @return PageResult 自定义，统一分页查询结果
     */
    public PageResult findByPage(TBizArtUser tBizArtUser, PageRequest pageRequest){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<TBizArtUser> list = tbaUserDao.findByPage(tBizArtUser);
        PageResult pageResult = PageUtils.getPageResult(pageRequest, new PageInfo<TBizArtUser>(list));
        return pageResult;
    }

    public boolean save(TBizArtUser tBizArtUser, HttpServletRequest request){
        UserManage user =(UserManage)request.getSession().getAttribute("session_user");
        tBizArtUser.setCreattime(DateUtil.formatDate(new Date()));
        tBizArtUser.setCreatuser(user.getId().toString());
        int i = tbaUserDao.save(tBizArtUser);
        if(i>0)
            return  true;
        else
            return false;
    }

    public boolean update(TBizArtUser tBizArtUser, HttpServletRequest request){
        UserManage user =(UserManage)request.getSession().getAttribute("session_user");
        tBizArtUser.setUpdatetime(DateUtil.formatDate(new Date()));
        tBizArtUser.setUpdateuser(user.getId().toString());
        int i = tbaUserDao.update(tBizArtUser);
        if(i>0)
            return  true;
        else
            return false;
    }

    public TBizArtUser findById(Integer id){
        TBizArtUser user =tbaUserDao.findById(id);
        return  user;
    }

    public boolean del(Integer id){
        int i = tbaUserDao.del(id);
        if(i>0)
            return true;
        else
            return false;
    }

}
